package com.liao.healthydiet.ui.activity;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.ActionBar;
import androidx.core.app.NotificationCompat;

import com.liao.healthydiet.BaseActivity;
import com.liao.healthydiet.R;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private Button btn_foot_list;
    private Button btn_foot_nutrition;
    private Button btn_foot_about;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        notification();
    }

    private void initView() {
        btn_foot_list = (Button) findViewById(R.id.btn_foot_list);
        btn_foot_nutrition = (Button) findViewById(R.id.btn_foot_nutrition);
        btn_foot_about = (Button) findViewById(R.id.btn_foot_about);

        btn_foot_list.setOnClickListener(this);
        btn_foot_nutrition.setOnClickListener(this);
        btn_foot_about.setOnClickListener(this);
    }

    private void notification() {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        PendingIntent pi = PendingIntent.getActivity(this, 0
                , new Intent(this, FoodListActivity.class), 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "通知");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel("通知", "n", NotificationManager.IMPORTANCE_DEFAULT);
            if (manager != null) {
                manager.createNotificationChannel(notificationChannel);
            }
        }
        builder.setContentTitle("标题")
                .setContentText("内容")
                .setSmallIcon(R.mipmap.icon)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pi);
        if (manager != null) {
            manager.notify(1, builder.build());
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.btn_foot_list:
                intent = new Intent(this, FoodListActivity.class);
                break;
            case R.id.btn_foot_nutrition:
                intent = new Intent(this, FoodNutritionActivity.class);
                break;
            case R.id.btn_foot_about:
                intent = new Intent(this, FoodAboutActivity.class);
                break;
        }
        if (intent != null) {
            startActivity(intent);
        }
    }
}
